package com.dd.stack.es.controller;

import com.dd.stack.es.pojo.es.ProductESDO;
import com.dd.stack.es.pojo.req.ProductESReq;
import com.dd.stack.es.service.internal.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author liuxianmeng
 * @CreateTime 2024/10/30 19:58
 * @Description 产品ES基础操作
 */
@Slf4j
@RestController
@Api(tags = "ES product操作")
@RequiredArgsConstructor
@RequestMapping("/products")
public class ProductESController {

    private final ProductService productService;

    @PostMapping("/add")
    @ApiOperation("保存产品")
    public ResponseEntity<ProductESDO> saveProduct(@ApiParam("要保存的产品信息") @RequestBody ProductESDO ProductESDO) {
        ProductESDO savedProduct = productService.add(ProductESDO);
        return new ResponseEntity<>(savedProduct, HttpStatus.CREATED);
    }

    @GetMapping("/id/{id}")
    @ApiOperation("根据id获取产品")
    public ResponseEntity<ProductESDO> findProductById(@ApiParam("产品id") @PathVariable String id) {
        ProductESDO ProductESDO = productService.get(id);
        if (ProductESDO!= null) {
            return new ResponseEntity<>(ProductESDO, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @GetMapping("/name/{name}")
    @ApiOperation("根据产品名称查询产品(V1版本分页查询)")
    public ResponseEntity<List<ProductESDO>> findProductsByName(@ApiParam("要查询的产品名称")@PathVariable String name) {
        List<ProductESDO> productESDOList = productService.getByName(name);
        if (!productESDOList.isEmpty()) {
            return new ResponseEntity<>(productESDOList, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @PostMapping("/search/page")
    @ApiOperation("全量查询(所有条件都考虑 返回分页信息)")
    public ResponseEntity<PageImpl<ProductESDO>> search(@ApiParam("product索引查询请求类 包含分页信息") @RequestBody ProductESReq req) {
        PageImpl<ProductESDO> curPage = productService.search(req);
        if (!curPage.getContent().isEmpty()) {
            return new ResponseEntity<>(new PageImpl<>(curPage.getContent()), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @PutMapping
    @ApiOperation("更新产品")
    public ResponseEntity<ProductESDO> updateProduct(@ApiParam("要更新的产品信息") @RequestBody ProductESDO ProductESDO) {
        ProductESDO updatedProduct = productService.updateProduct(ProductESDO);
        return new ResponseEntity<>(updatedProduct, HttpStatus.OK);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除产品")
    public ResponseEntity<Void> deleteProductById(@ApiParam("要删除的产品id") @PathVariable String id) {
        productService.deleteProductById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
